﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Data;

using Visifire.Charts;
using Visifire.Commons;
using Safe_Driving.TokenUser;
using System.Net.Mail;


namespace Safe_Driving
{
    public partial class MainWindow
    {
        private void initClient()
        {
            canvNouveauPermit.Visibility = System.Windows.Visibility.Hidden;

            SingletonTokenUser token = SingletonTokenUser.GetInstance();
            AppSafeDriving app = (AppSafeDriving)token.DbApp;

            SFClient cli = (from c in app.SFClient where c.IDClient == token.Client_selected.IDClient select c).SingleOrDefault();

            CliName.Content = "Nom : " + cli.NoM.ToUpper();
            CliPrenom.Content = "Prenom : " + cli.PreNoM;
            CliNaissance.Content = "Date de Naissance : " + cli.DateNAiSsaNcE.ToString();// attention elever les heures
            CliAdress.Content = "Adresse : " + cli.ADressE;
            CliPostal.Content = cli.CP;
            CliVille.Content = cli.VIllE.ToUpper();
            CliPortable.Content = "Telephone Portable : " + cli.TeLPortable.ToString();
            CliFix.Content = "Telephone Fixe : " + cli.TeLFixE.ToString();
            CliMail.Content = "Email : " + cli.Email;

            


            IEnumerable<SFPerMis> permit = (from c in app.SFPerMis where c.IDClient == token.Client_selected.IDClient select c);
            dataGridpermit.ItemsSource = permit;



            AppSafeDriving dbApp = SingletonTokenUser.GetInstance().DbApp;

            IEnumerable<SFClient> q = (from sfClient in dbApp.SFClient
                                       select sfClient);
            dataGrid_selectClient.ItemsSource = q;

            //Controle du graph
            Graph(token.Client_selected.IDClient);

            //goToTab(Ajout_scroe_TabItem); BOUTON POUR ALLER AU SCORE

        }

        public void Graph(int idClient)
        {
            // Set chart width and height

            chart.Width = 400;
            chart.Height = 300;

            // Create new DataSeries
            DataSeries dataSeries = new DataSeries();


            SingletonTokenUser token = SingletonTokenUser.GetInstance();
            AppSafeDriving app = (AppSafeDriving)token.DbApp;

            IEnumerable<SFInSCrIt> ins = (from c in app.SFInSCrIt where c.IDClient == idClient select c);


            // Number of DataPoints to be generated
            int numberOfDataPoints = ins.Count();

            // Loop and add a few DataPoints

            for (int loopIndex = 0; loopIndex < numberOfDataPoints; loopIndex++)
            {
                // Create a DataPoint

                DataPoint dataPoint = new DataPoint();

                // Set the YValue using random number
                if (ins.ElementAt(loopIndex).Note != null)
                {
                    dataPoint.YValue = (double)ins.ElementAt(loopIndex).Note;
                    string dateducour = ins.ElementAt(loopIndex).DateDebut.Day.ToString() + "/" + ins.ElementAt(loopIndex).DateDebut.Month.ToString() + "/" + ins.ElementAt(loopIndex).DateDebut.Year.ToString();
                    dataPoint.AxisXLabel = dateducour;
                }
                else
                {
                    dataPoint.YValue = 0;
                    string dateducour = ins.ElementAt(loopIndex).DateDebut.Day.ToString() + "/" + ins.ElementAt(loopIndex).DateDebut.Month.ToString() + "/" + ins.ElementAt(loopIndex).DateDebut.Year.ToString();
                    dataPoint.AxisXLabel = dateducour + "Pas encore notée";
                }

               

                // Add DataPoint to DataSeries

                dataSeries.DataPoints.Add(dataPoint);

            }


            // Add DataSeries to Chart
            chart.Series.Clear();
            chart.Series.Add(dataSeries);

        }

        #region Ajouter Permit

        /// <summary>
        /// Rend visible le panel : Ajouter un permit a un utilisateur
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPermit_Click(object sender, RoutedEventArgs e)
        {

            //Rend le panel Visible
            canvNouveauPermit.Visibility = System.Windows.Visibility.Visible;

            //comboTypePermit Affiche les differant type de permit
            int i = 0;

            foreach (var item in listePermit())
            {
                comboTypePermit.Items.Add(listePermit()[i]);
                i++;
            }
            //Remplie la combo nombre de point
            comboNbPoint.Items.Clear();
            for (int j = 0; j < 13; j++)
            {
                comboNbPoint.Items.Add(j);
            }


        }
        /// <summary>
        /// Ajouter un permit dans la base de donnée
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAjouterPermit_Click(object sender, RoutedEventArgs e)
        {
            SingletonTokenUser token = SingletonTokenUser.GetInstance();
            AppSafeDriving app = (AppSafeDriving)token.DbApp;
            List<SFPerMis> ls_permit = new List<SFPerMis>();

            SFPerMis permit = new SFPerMis();
            permit.IDClient = token.Client_selected.IDClient;
            permit.NoM = comboTypePermit.SelectedItem.ToString();
            permit.OBTentIon = (DateTime)datePicker1.SelectedDate;
            permit.NBPoints = (int)comboNbPoint.SelectedItem;

            ls_permit.Add(permit);

            app.SFPerMis.InsertAllOnSubmit(ls_permit);

            app.SubmitChanges();
            IEnumerable<SFPerMis> permit1 = (from c in app.SFPerMis where c.IDClient == token.Client_selected.IDClient select c);
            dataGridpermit.ItemsSource = permit1;

        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            SingletonTokenUser token = SingletonTokenUser.GetInstance();
            AppSafeDriving app = (AppSafeDriving)token.DbApp;
            SFPerMis permit = new SFPerMis();

            permit = (SFPerMis)dataGridpermit.SelectedItem;
            MessageBox.Show("Le permit a bien eté suprimé");
            app.SFPerMis.DeleteOnSubmit(permit);
            app.SubmitChanges();

            IEnumerable<SFPerMis> permit0 = (from c in app.SFPerMis where c.IDClient == token.Client_selected.IDClient select c);
            dataGridpermit.ItemsSource = permit0;
        }

        #endregion

        /// <summary>
        /// Permet de crée une liste avec differant type de permit
        /// </summary>
        /// <returns>List<string> des differant type de permit</returns>
        private List<string> listePermit()
        {
            List<string> permit = new List<string>();
            permit.Add("Permit B (voiture)");
            permit.Add("Permit A (moto)");
            permit.Add("Code de la route");
            permit.Add("Conduite sur glace");
            return permit;

        }


        private void button1_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("coucou");
            EnvoieMail("celine@business-action.com");
        }


        public bool EnvoieMail(string email)
        {
            try
            {
                MailMessage myMail = new MailMessage();
                MailAddress myadress = new MailAddress("97362@supinfo.com");
                MailAddress destinataireadress = new MailAddress("celine@business-ac.com");
                myMail.Subject = "sujet du mail";
                myMail.From = myadress;
                myMail.Sender = myadress;
                myMail.Body = "corps du texte";

                SmtpClient smtp = new SmtpClient("smpt.supinfo.com");
                smtp.Credentials = new System.Net.NetworkCredential("user", "pass");
                smtp.Send(myMail);

                bool ok = true;
                return ok;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                throw;
            }

        }


    }


}
